package com.net.tool;

import java.util.Vector;

import android.os.Handler;

public class BgkTaskMange {

	Vector<PostTask> mBufferPostTasks;
	Vector<DownLoadTask> mBufferLoadTasks;
	Vector<AsyncCommend> mBufferAsyncCommends;

	PostTask mCurrentTask;

	boolean mAlive = true;

	private BgkTaskMange() {
		mBufferPostTasks = new Vector<PostTask>();
		mBufferLoadTasks = new Vector<DownLoadTask>();
		mBufferAsyncCommends = new Vector<AsyncCommend>();
		mExecutePostTasks = new ExecutePostTasks();
		mExecuteCommandTask = new ExecuteCommandTask();
		mHandler = new Handler();
		new Thread(mExecutePostTasks).start();
		new Thread(mExecuteCommandTask).start();

	}

	private static BgkTaskMange mBgkTaskMange;

	public static BgkTaskMange getInstance() {

		if (mBgkTaskMange == null) {
			mBgkTaskMange = new BgkTaskMange();
		}

		return mBgkTaskMange;

	}

	private Handler mHandler;

	private void runningPost(final PostTask postTask) {

		postTask.gbkDo();

		mHandler.post(new Runnable() {
			public void run() {
				postTask.gbkPostUI();
			}
		});
	}

	ExecutePostTasks mExecutePostTasks;

	public void executePostTask(final PostTask postTask) {

		if (postTask == null) {

			return;
		}

		if (mBufferPostTasks.indexOf(postTask) == -1) {

			mHandler.post(new Runnable() {
				PostTask mPostTask = postTask;

				public void run() {
					mPostTask.gbkPrevUI();
				}
			});

			mBufferPostTasks.addElement(postTask);
			mExecutePostTasks.execute();

		}

		if (mCurrentTask != null && mCurrentTask instanceof DownLoadTask) {

			DownLoadTask downLoadTask = (DownLoadTask) mCurrentTask;
			if (mBufferLoadTasks.indexOf(downLoadTask) == -1) {
				mBufferLoadTasks.addElement(downLoadTask);
			}
			executeCommend(downLoadTask);
		}

	}

	public void executeDownloadTask(DownLoadTask downLoadTask) {

		mBufferLoadTasks.addElement(downLoadTask);
		mExecutePostTasks.execute();

	}

	ExecuteCommandTask mExecuteCommandTask;

	public void executeCommend(AsyncCommend asyncCommend) {
		mBufferAsyncCommends.addElement(asyncCommend);
		mExecuteCommandTask.execute();
	}

	class ExecutePostTasks implements Runnable {

		String mLock;
		boolean mIsLock = false;

		public ExecutePostTasks() {
			mLock = "ExecutePostTasks";
		}

		public void execute() {

			synchronized (mLock) {
				if (mIsLock) {
					mLock.notify();
					mIsLock = false;
				}
			}

		}

		protected boolean gbkRun() {

			if (mBufferPostTasks.size() > 0) {
				PostTask postTask = mBufferPostTasks.firstElement();

				if (postTask != null) {
					mCurrentTask = postTask;
					runningPost(postTask);
					mBufferPostTasks.removeElement(postTask);
				} else {
					mBufferPostTasks.removeElementAt(0);
				}

				return true;
			}

			if (mBufferLoadTasks.size() > 0) {

				DownLoadTask downLoadTask = mBufferLoadTasks.firstElement();

				if (downLoadTask != null) {
					mCurrentTask = downLoadTask;
					runningPost(downLoadTask);
					mBufferLoadTasks.removeElement(downLoadTask);
				} else {
					mBufferLoadTasks.removeElementAt(0);
				}

				return true;
			}

			return false;

		}

		public void run() {

			while (mAlive) {

				if (!gbkRun()) {

					synchronized (mLock) {

						try {
							mIsLock = true;
							mLock.wait();
						} catch (InterruptedException e) {

							e.printStackTrace();
						}
					}
				}

			}

		}
	}

	class ExecuteCommandTask extends ExecutePostTasks {

		public ExecuteCommandTask() {
			mLock = "ExecuteCommandTask";

		}

		protected boolean gbkRun() {
			if (mBufferAsyncCommends.size() > 0) {
				AsyncCommend asyncCommend = mBufferAsyncCommends.firstElement();
				if (asyncCommend != null) {
					asyncCommend.asyncCommend();
					mBufferAsyncCommends.removeElement(asyncCommend);
				} else {
					mBufferAsyncCommends.removeElementAt(0);
				}

				return true;
			}

			return false;

		}
	}

}
